package internationalenergytradingplatform.controller.BuyerController;

import internationalenergytradingplatform.VO.BuyerLoginVO;
import internationalenergytradingplatform.constant.MessageConstant;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.BuyerAccountDao;
import internationalenergytradingplatform.exception.AccountLockedException;
import internationalenergytradingplatform.exception.AccountNotFoundException;
import internationalenergytradingplatform.exception.PasswordErrorException;
import internationalenergytradingplatform.pojo.BuyerAccount;
import internationalenergytradingplatform.properties.JwtProperties;
import internationalenergytradingplatform.result.Result;
import internationalenergytradingplatform.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/buyer-account")
public class BuyerAccountController {

    @Autowired
    BuyerAccountDao buyerAccountDao;

    @Autowired
    private JwtProperties jwtProperties;
    /**
     *
     * @author yingfeng
     * @date 2024/3/20 14:38
     采购商登录
     */
    @PostMapping("/login")
    public Result<BuyerLoginVO> login(@RequestBody BuyerAccount buyerAccountDTO){
         String tele = buyerAccountDTO.getTele();
         String password = buyerAccountDTO.getPassword();
        BuyerAccount buyerByTele = buyerAccountDao.getBuyerByTele(tele);
        //如果账号不存在
        if(buyerByTele == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //对密码md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //密码错误
        if (!password.equals(buyerByTele.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //账号被锁定
        if(buyerByTele.getBuyerAccountrStatus() != 1) {
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("tele", buyerByTele.getTele());
        String token = JWTUtil.createJWT(
                jwtProperties.getAdminSecretKey(),//令牌的key
                jwtProperties.getAdminTtl(),//令牌的有效时间
                claims);
        //将采购商信息写入VO中
        BuyerLoginVO buyerLoginVO = BuyerLoginVO.builder()
                .tele(buyerByTele.getTele())
                .password(buyerByTele.getPassword())
                .mpanyName(buyerByTele.getMpanyName())
                .unifiedCreditCode(buyerByTele.getUnifiedCreditCode())
                .qualificationCertificateIma(buyerByTele.getQualificationCertificateIma())
                .avatarImage(buyerByTele.getAvatarImage())
                .buyerAccountrStatus(buyerByTele.getBuyerAccountrStatus())
                .balance(buyerByTele.getBalance())
                .address(buyerByTele.getAddress())
                .token(token)
                .build();
        log.info("登录结果：{}", buyerLoginVO);
        return Result.success(buyerLoginVO);
    }

    /**
     * 采购商修改地址
     */
    @PutMapping("/address")
    public Result updateAddress(String address){
        String tele = BaseContext.getCurrentId();//获取手机号
        buyerAccountDao.updateAddress(tele, address);
        return Result.success();
    }

    /**
     *
     * @author yingfeng
     * @date 2024/4/26 17:41
    采购商获取余额
     */
    @GetMapping("/balance")
    public Result<Double> getBalance(){
        String tele = BaseContext.getCurrentId();//获取手机号
        Double buyerBalance = buyerAccountDao.getBuyerBalance(tele);
        return Result.success(buyerBalance);
    }

}
